﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Zor_IDAL;
using Zor_Modes;

namespace Zor_DAL
{
    public class TaxRecord_DAL
    {
        /// <summary>
        /// 获取用户缴税信息
        /// </summary>
        /// <param name="type">租户类型（1:个人   2:企业）</param>
        /// <param name="name">业主名称/单位名称</param>
        /// <param name="number">身份证号/税号</param>
        /// <returns></returns>
        public static List<TaxRecord> GetTaxRecordList(int type,string name,string number)
        {
            List<TaxRecord> lst=new List<TaxRecord>();
            var sql = "select * from V_GetTaxRecord where 1=1 ";
            if (type == 1)
            {
                sql += " and Tenant_Type= " + type + " and Tenant_Name= '" + name + "' and Tenant_IDNumber= '" + number + "' ";
                lst = DapperQuery<TaxRecord>.DoQueryList("1", sql, null);
            }
            if (type == 2)
            {
                sql += " and Tenant_Type= " + type + " and Tenant_Name= '" + name + "' and Tenant_TaxRegistrationNum= '" + number + "' ";
                lst = DapperQuery<TaxRecord>.DoQueryList("1", sql, null);
            }
            return lst;
        }

        /// <summary>
        /// 纳税预警信息
        /// </summary>
        /// <param name="type">租户类型（1:个人   2:企业）</param>
        /// <param name="name">租户姓名</param>
        /// <param name="number">租户身份证号</param>
        /// <returns></returns>
        public static List<TaxRecord> GetTaxRecord(int type, string name, string number)
        {
            List<TaxRecord> lst = new List<TaxRecord>();
            //提前几天预警
            int day = int.Parse(ConfigurationManager.AppSettings["day"].ToString());
            DateTime now = DateTime.Now;
            DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0);
            //租房结束时间对比时间（租房结束时间大于下个月第一天,满足该条件说明房屋还在继续租用中）
            int endOfRentalTime = Convert.ToInt32((new DateTime(now.Year, now.Month , 1).AddMonths(1) - dateStart).TotalSeconds);
            //缴税结束时间对比时间（缴税结束时间小于当前月最后一天,满足该条件说明需继续缴税）
            int endOfTax = Convert.ToInt32((new DateTime(now.Year, now.Month, 1).AddMonths(1) - dateStart).TotalSeconds);
            //上次缴税时间对比时间（上次缴税时间小于下个月第一天减提前预警的天数）
            int lastTaxTime = Convert.ToInt32(((new DateTime(now.Year, now.Month, 1).AddMonths(1)).AddDays(-(day)) - dateStart).TotalSeconds);
           
            var sql = @"
                        select * 
                        from 
                            V_GetTaxRecord 
                        where
                            Tenant_Name= '" + name + "' and Tenant_IDNumber= '" + number + "' and Tenant_Type= " + type + " and Lease_EndTime > " + endOfRentalTime + " and Acceptance_TaxEndTime < " + endOfTax + " and Pt_Date <= " + lastTaxTime;

            lst = DapperQuery<TaxRecord>.DoQueryList("1", sql, null);
            
            return lst;
        }
    }
}
